package com.zyx.javademo.stream;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Yaxi.Zhang
 * @since 2021/12/13 14:02
 * desc: Stream流的聚合
 */
public class StreamAggDemo {
    public static void main(String[] args) {
        // 依据自己进行groupingBy
        System.out.println(Stream.of("aa", "cc", "aa", "bb").collect(Collectors.groupingBy(Function.identity(), Collectors.toList())));
        // wordCount 使用默认的Collectors.counting()统计个数为long类型
        Map<String, Long> wc1 = Stream.of("aa", "cc", "aa", "bb").collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        System.out.println(wc1);
        // wordCount 使用reducing统计int类型的结果
        Map<String, Integer> wc2 = Stream.of("aa", "cc", "aa", "bb").collect(Collectors.groupingBy(Function.identity(), Collectors.reducing(0, e -> 1, Integer::sum)));
        System.out.println(wc2);
        // 使用joining拼接字符串
        System.out.println(Stream.of("aa", "bb", "cc").map(x -> x + "1").collect(Collectors.joining(",")));

    }
}
